##################################################################
#------------- Replication Figures: SEAL THE DEAL ---------------
##################################################################

#------------------
# Packages
#------------------

library(ggplot2)
library(stargazer)
library(survival)
library(gridExtra)
library(ggrepel)
library(plyr)

#------------------
# Paths
#------------------
setwd("~/replication/")
#------------------
# Load Dataset
#------------------
dat <- readRDS("data_second_stage.rds")

#------------------
# Figure 1: Negotiation duration in days
#------------------
dat$Label <- ifelse(dat$negotiationduration>3360,paste(as.character(dat$name),dat$Year),"") #if we also want Canada Korea then we must put >3350
dat$Label <- ifelse(dat$negotiationduration<74,paste(as.character(dat$name),dat$Year),dat$Label)

dat$x <- 1
ggplot(dat, aes(x,as.numeric(as.character(negotiationduration)),label=Label)) + 
  geom_boxplot()+geom_jitter(aes(x,as.numeric(as.character(negotiationduration))),
                             position=position_jitter(width=0.1,height=0),
                             alpha=0.6,
                             size=2.5,
                             show_guide=FALSE,color="gray") + 
  geom_text_repel(data=dat[dat$Label!="",],size=4,show.legend = FALSE)+
  ylab("Negotiation Duration in Days")+
  xlab("")+
  theme_bw()+theme(
    axis.title=element_text(size=16),
    axis.text=element_text(size=14),
    axis.text.x=element_blank())

#------------------
# Figure 2: Difference in design templates
#------------------
dat$difave_max<-ifelse(dat$difave_max>1,1,dat$difave_max)
dat$Label <- ifelse((dat$difave_max)>0.995,paste(as.character(dat$name),dat$Year),"")
dat$Label <- ifelse((dat$difave_max)<0.09,paste(as.character(dat$name),dat$Year),dat$Label)
dat$Label <- ifelse(dat$number==96|dat$number==129|dat$number==813|dat$number==814|dat$number==323,paste(as.character(dat$name),dat$Year),dat$Label)

dat$Label <- ifelse(dat$number==347|dat$number==96|dat$number==508,paste(as.character(dat$name),dat$Year),"")
dat$Label <- ifelse((dat$difave_max)<0.09,paste(as.character(dat$name),dat$Year),dat$Label)

dat$x <- 1
ggplot(dat, aes(x,as.numeric(as.character(difave_max)),label=Label)) + 
  geom_boxplot()+geom_jitter(aes(x,as.numeric(as.character(difave_max))),
                             position=position_jitter(width=0.1,height=0),
                             alpha=0.6,
                             size=2.5,
                             show_guide=FALSE,color="gray") + 
  geom_text_repel(data=dat[dat$Label!="",],size=4,show.legend = FALSE)+
  ylab("Difference in Design Templates (Transaction Costs)")+
  xlab("")+
  theme_bw()+theme(
    axis.title=element_text(size=16),
    axis.text=element_text(size=14),
    axis.text.x=element_blank()
  )
#------------------
# Figure 3: Relative scope
#------------------
dat$Label <- ifelse((dat$acr_max)>0.45,paste(as.character(dat$name),dat$Year),"")
dat$Label <- ifelse(dat$number==911|dat$number==636|dat$number==611,paste(as.character(dat$name),dat$Year),"")
dat$Label <- ifelse(dat$number==220|dat$number==919|dat$number==836,paste(as.character(dat$name),dat$Year),dat$Label)
dat$acr_max<-ifelse(dat$number==220,dat$acr_max+0.02,dat$acr_max)
dat$acr_max<-ifelse(dat$number==919,dat$acr_max+0.04,dat$acr_max)
dat$x <- 1
ggplot(dat, aes(x,as.numeric(as.character(acr_max)),label=Label)) + 
  geom_boxplot()+geom_jitter(aes(x,as.numeric(as.character(acr_max))),
                             position=position_jitter(width=0.1,height=0),
                             alpha=0.6,
                             size=2.5,
                             show_guide=FALSE,color="gray") + 
  geom_text(data=dat[dat$Label!="",],size=4,show.legend = FALSE)+
  ylab("Relative Scope")+
  xlab("")+
  theme_bw()+theme(
    axis.title=element_text(size=16),
    axis.text=element_text(size=14),
    axis.text.x=element_blank()
  )
#------------------
# Figure 4: Relative scope and negotiation duration for autocracies and democracies
#------------------

#split it for regime types
dat$RegimeType <- ifelse(dat$polity2ave>=18,"Full democracies","Not full democracies")
dat2 <- dat[!is.na(dat$polity2ave),]


dat2$Label<-ifelse(dat2$acr_max>0.3&dat2$polity2ave<13,paste(dat2$name,dat2$Year),"")
dat2$Label<-ifelse((dat2$acr_max>0.3&dat2$polity2ave==20|dat2$negotiationduration>2000&dat2$polity2ave==20|dat2$negotiationduration>5000),paste(dat2$name,dat2$Year),dat2$Label)
table(dat2$Label!="")
dat2$Label<-ifelse((dat2$negotiationduration>2200 & dat2$negotiationduration<3100 & dat2$acr_max==0 & dat2$RegimeType=="Autocracy"),paste(dat2$name,dat2$Year),dat2$Label)
dat[dat$acr_max>0.4,c("name","Year","polity2ave")]
dat2 <- dat2[!is.na(dat2$polity2ave),]
ggplot(dat2, aes(as.numeric(as.character(acr_max)),negotiationduration,label=Label))+
  geom_point(aes(shape=RegimeType),size=2.5,color="gray",alpha=0.5)+
  geom_smooth(aes(linetype=RegimeType),method="lm",se=F,color="darkgray")+
  geom_text_repel(data=dat2[dat2$Label!="",],size=4,show.legend = FALSE)+
  #scale_shape_manual(values=c(21,22))+
  ylab("Negotiation Duration")+
  xlab("Relative Scope")+
  facet_grid(.~RegimeType)+
  theme_bw()+theme(
    axis.title=element_text(size=16),
    axis.text=element_text(size=14),
    legend.position = "none",
    strip.text=element_text(size=14))

